package com.iflytek.read.util;

import android.text.TextUtils;
import android.util.Log;

import com.iflytek.read.BuildConfig;
import com.iflytek.read.bean.UserInfo;

import java.io.File;
import java.util.List;

import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.Request;
import okhttp3.RequestBody;

/**
 * @author wangshuai
 */
@SuppressWarnings("ALL")
public class MultipartBuilderHelper {

    private static final String TAG = "MultipartBuilderHelper";
    public static final String BASE_URL = BuildConfig.BASE_URL;

    private final UserInfo userInfo;

    public MultipartBuilderHelper(UserInfo userInfo) {
        Log.i(TAG, "BASE_URL  ==  " + BASE_URL);
        this.userInfo = userInfo;
    }

    /**
     * 创建基础Builder
     * @return  返回值
     */
    public MultipartBody.Builder createBaseBuilder() {
        if (userInfo == null) {
            throw new IllegalStateException("UserInfo cannot be null");
        }

        return new MultipartBody.Builder()
                .setType(MultipartBody.FORM)
                .addFormDataPart("userId", userInfo.getUserId())
                .addFormDataPart("userName", userInfo.getUserName());
    }

    /**
     * 添加文件
     * @param builder   Builder 对象
     * @param files 文件列表
     */
    public void addFiles(MultipartBody.Builder builder, List<File> files) {
        if (files == null || files.isEmpty()) {
            throw new IllegalArgumentException("Files list cannot be null or empty");
        }

        for (File file : files) {
            if (file.exists()) {
                MediaType mediaType = MediaType.parse("multipart/form-data");
                builder.addFormDataPart(
                        "files",
                        file.getName(),
                        RequestBody.create(mediaType, file)
                );
            }
        }
    }

    /**
     * 添加可选参数
     * @param builder   参数
     * @param name      key
     * @param value     value
     * @param defaultValue  默认值
     */
    public void addOptionalParam(MultipartBody.Builder builder, String name, String value, String defaultValue) {
        builder.addFormDataPart(name, !TextUtils.isEmpty(value) ? value : defaultValue);
    }

    /**
     * 创建请求Builder
     * @param url   请求接口链接
     * @param multipartBuilder  参数
     * @return  返回
     */
    public Request.Builder createRequestBuilder(String url, MultipartBody.Builder multipartBuilder) {
        Request.Builder requestBuilder = new Request.Builder()
                .url(BASE_URL + url)
                .addHeader("System-Code", "0")
                .addHeader("Current-Org-Id", "1104010")
                .addHeader("user-defined", userInfo.getToken())
                .addHeader("Content-Type", "multipart/form-data");

        if (multipartBuilder != null) {
            requestBuilder.post(multipartBuilder.build());
        }

        return requestBuilder;
    }

    /**
     * 创建JSON请求Builder
     * @param url   网络接口地址
     * @param jsonBody  json格式数据
     * @return  返回值
     */
    public Request.Builder createJsonRequestBuilder(String url, String jsonBody) {
        if (TextUtils.isEmpty(jsonBody)) {
            throw new IllegalArgumentException("JSON body cannot be null or empty");
        }

        return new Request.Builder()
                .url(BASE_URL + url)
                .addHeader("System-Code", "0")
                .addHeader("Current-Org-Id", "1104010")
                .addHeader("user-defined", userInfo.getToken())
                .addHeader("Content-Type", "application/json")
                .post(RequestBody.create(jsonBody, MediaType.parse("application/json; charset=utf-8")));
    }

    /**
     * 创建通用请求Builder
     * @param url   接口链接
     * @param method    请求方式，get、post、put、delete、patch 等待
     * @return  返回值
     */
    public Request.Builder createRequestBuilder(String url, String method) {
        return new Request.Builder()
                .url(BASE_URL + url)
                .addHeader("System-Code", "0")
                .addHeader("Current-Org-Id", "1104010")
                .addHeader("user-defined", userInfo.getToken())
                .method(method, null);
    }
}